package projectServlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOException;
import dao.EmployeeDAOFactory;
import dao.LeaveDAOFactory;
import dao.mysql.EmployeeDAOFactoryImpl;
import dao.mysql.LeaveDAOFactoryImpl;
import dto.LeaveDTO;
import dto.UserDTO;

/**
 * Servlet implementation class ManagerPage
 */
@WebServlet("/ManagerPage")
public class ManagerPage extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManagerPage() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
		// TODO Auto-generated method stub
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		{
			String approveButton = request.getParameter("approve");
			String cancleButton = request.getParameter("cancle");

			String SUserID = request.getParameter("ApplicantID");
			int UserID = Integer.parseInt(SUserID);

			String SLeaveID = (String) request.getParameter("LeaveID");
			int LeaveID = Integer.parseInt(SLeaveID);			
			
			LeaveDAOFactory lDAO = new LeaveDAOFactoryImpl();

			if (approveButton != null && approveButton.equals("Approve")) {
				try {
					lDAO.getLeaveDAO().setLeaveStatus("Approved", "", LeaveID);
					
					EmployeeDAOFactory   eFactory = new EmployeeDAOFactoryImpl();
					UserDTO				 user	  = new UserDTO();
					LeaveDTO			 leave    = new LeaveDTO();
					double Adays = 0,Mdays = 0,Cdays = 0;
					
					user  = eFactory.getEmployeeDAO().findUser(UserID);
					leave = lDAO.getLeaveDAO().findLeave(LeaveID);
					if(leave.get_leaveType().equals("Annual"))
					{
						Adays = user.get_annualLeave() - leave.get_LeaveSpent();
						Mdays = user.get_medicalLeave();
						Cdays = user.get_compensationLeave();
					}
					else if(leave.get_leaveType().equals("Medical"))
					{
						Adays = user.get_annualLeave();
						Mdays = user.get_medicalLeave() - leave.get_LeaveSpent();
						Cdays = user.get_compensationLeave();				
					}
					else if(leave.get_leaveType().equals("Compensation"))
					{
						Adays = user.get_annualLeave();						
						Mdays = user.get_medicalLeave();
						Cdays = user.get_compensationLeave() - leave.get_LeaveSpent();
						
					}
					eFactory.getEmployeeDAO().updateUserLeaveDays(UserID, Adays, Mdays,Cdays);
				} catch (DAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (cancleButton != null && approveButton.equals("Reject")) {
				String RejectReason = request.getParameter("rejectreason");
				try {
					lDAO.getLeaveDAO().setLeaveStatus("Rejected", RejectReason,
							LeaveID);
				} catch (DAOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			RequestDispatcher rd = request
					.getRequestDispatcher("LeaveHistoryForM.jsp");
			rd.forward(request, response);
		}
	}
}
